//package com.wx.wxpush.job;
//
//
//import cn.hutool.cron.CronConfig;
//import com.wx.wxpush.entiy.Cron;
//import com.wx.wxpush.mapper.CornTaskMapper;
//import com.wx.wxpush.service.PushService;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.jdbc.core.JdbcTemplate;
//import org.springframework.scheduling.SchedulingException;
//import org.springframework.scheduling.TaskScheduler;
//import org.springframework.scheduling.Trigger;
//import org.springframework.scheduling.TriggerContext;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.scheduling.annotation.SchedulingConfigurer;
//import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
//import org.springframework.scheduling.config.ContextLifecycleScheduledTaskRegistrar;
//import org.springframework.scheduling.config.ScheduledTaskRegistrar;
//import org.springframework.scheduling.config.Task;
//import org.springframework.scheduling.config.TriggerTask;
//import org.springframework.scheduling.support.CronTrigger;
//
//import java.time.LocalDateTime;
//import java.util.*;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.ScheduledFuture;
//
//@Configuration
//@EnableScheduling
//public class ScheduledConfig implements SchedulingConfigurer {
//    //笔者这里配合数据库来实现动态添加。方便查询数据库这里使用JdbcTemplate
//    @Autowired
//    JdbcTemplate jdbcTemplate;
//    @Autowired
//    private PushService pushService;
//
//    @Autowired
//    private ScheduledConfig scheduledConfig;
//    private ScheduledTaskRegistrar taskRegistrar;
//
//    private Set<ScheduledFuture<?>> scheduledFutures = null;
//    private Map<String, ScheduledFuture<?>> taskFutures = new ConcurrentHashMap<>();
//
//    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
//
//    private static List tasks;
//
//    private static boolean initialized = false;
//
//    @Autowired
//    private CornTaskMapper cornTaskMapper;
//
//    ScheduledTaskRegistrar scheduledTaskRegistrar = new ScheduledTaskRegistrar();
//    Cron cron = new Cron();
//
////    public List<Map<String, Object>> selectList(){
////        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select openid,id,cronmoring,cornnight from cron ");
////        return maps;
////    }
//
//    //    @Scheduled(cron = "0/10 * * * * ?")
////    public List<Map<String, Object>> bTask() {
////        System.out.println("自动定时");
////        try {
////            if (initialized) {
////                threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(false);
////                threadPoolTaskScheduler.shutdown();
////
////            }
////        } catch (Exception e) {
////            e.printStackTrace();
////        } finally {
////            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select openid,id,cronmoring,cornnight from cron ");
////            System.out.println(maps+"maps");
////            return maps;
////            //System.out.println(cornTaskMapper.selectAllTask()+"任务");
////        }
////
////    }
//    List<Map<String, Object>> maps = new LinkedList<>();
//
//    @Scheduled(cron = "* * * * * ?")
//    public void bTask() {
//        try {
//            if (initialized) {
//                threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(false);
//                threadPoolTaskScheduler.shutdown();
//            }
//
//        } catch (Exception e) {
//
//            e.printStackTrace();
//
//        } finally {
//            maps = jdbcTemplate.queryForList("select cronmoring from cron ");
//            tasks = cornTaskMapper.selectAllTask();
//            scheduledTaskRegistrar.setCronTasksList(tasks);
//            scheduledConfig.configureTasks(scheduledTaskRegistrar);
//            //configureTasks(scheduledTaskRegistrar);
//        }
//
//
//    }
//
//    //    @Scheduled(cron = "0/50 * * * * ?")
////    public void update(){
////        cron.setCronmoring(cornTaskMapper.selectAllTask1( ));
////        System.out.println(cron.getCronmoring()+"getCronmoring");
////        System.out.println("改了"+cornTaskMapper.selectAllTask1());
////    }
////    @Override
////    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
////
////        if (tasks == null) {
////            bTask();
////        } else {
////            configureTasks( tasks );
////
////        }
////
////    }
////    public void configureTasks(List tasks) {
////
////        threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
////
////        threadPoolTaskScheduler.setPoolSize( 100 );//线程池大小
////
////        threadPoolTaskScheduler.setThreadNamePrefix( "configureTasks-" );
////
////        threadPoolTaskScheduler.setAwaitTerminationSeconds( 60 );//线程等待時間
////
////        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown( true );//等待所有任務執行完在銷毀
////
////        threadPoolTaskScheduler.initialize();
////
////        initialized = true;
////
////        if (taskRegistrar != null)
////
////            taskRegistrar.destroy();
////
////        taskRegistrar = new ContextLifecycleScheduledTaskRegistrar();
////
////        taskRegistrar.setTaskScheduler( threadPoolTaskScheduler );
////
////
////        for (Task task : tasks) {
////
////            Runnable runnable = new Runnable() {
////
////                @Override
////
////                public void run() {
////
////                    List list = cornTaskMapper.selectAllTask();
////
////                    if (!StringUtils.isEmpty( task.getCron() ) && task.getRepeat().equals( "1" )) {
////
////                        savaAppData( list, task );
////
////                    } else if (task.getRepeat().equals( "0" )) {
////
////                        if (isLong( task.getCron() )) {
////
////                            long num = System.currentTimeMillis() - Long.valueOf( task.getCron() );
////
////                            Timer timer = new Timer();
////
////                            timer.schedule( new TimerTask() {
////
////                                @Override
////
////                                public void run() {
////
////                                    savaAppData( list, task );
////
////                                }
////
////                            }, num );
////
////                        }
////
////                    }
////
////                }
////
////            };
////
////            Trigger trigger = new Trigger() {
////
////                @Override
////
////                public Date nextExecutionTime(TriggerContext triggerContext) {
////
////                    CronTrigger cronTrigger = new CronTrigger( task.getCron() );
////
////                    return cronTrigger.nextExecutionTime( triggerContext );
////
////                }
////
////            };
////
////            TriggerTask triggerTask = new TriggerTask( runnable, trigger );
////
////            taskRegistrar.setTriggerTasksList( Collections.singletonList( triggerTask ) );
////
////            taskRegistrar.afterSingletonsInstantiated();
////
////        }
////
////    }
////    }
//
//
//@Override
//    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
//
//        List<TriggerTask> list = new ArrayList<>();
//        //在注册器添加定时任务前添加线程池
//        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
//
//
//        //查询出来当前数据库中存储的所有有效的任务
//         List<Map<String, Object>> maps = jdbcTemplate.queryForList("select openid,id,cronmoring,cornnight from cron ");
////        System.out.println(maps);
//        //List<Map<String, Object>> maps =scheduledConfig.bTask();
//
//        System.out.println(maps + "mapsdssss111111111111");
//        //循环添加任务
//        maps.forEach(t -> {
//            TriggerTask triggerTask = new TriggerTask(() -> {
//                System.out.println("执行定时任务：" + LocalDateTime.now().toLocalTime());
//                // pushService.goodMorning();
//
//            }, triggerContext -> {
//                // System.out.println("执行Cron早上的:"+t.get("cronmoring").toString()+",id="+t.get("id").toString()+",openid="+t.get("openid").toString());
//                //如果需要动态的指定当前定时任务的执行corn。这里可以增加一步，查询数据库操作。如果任务corn不需要精确修改，corn可进行缓存。到期在去查询数据库。这里根据读者的需求自行取舍。
//
//                System.out.println("近期这个");
////                if(t.get("cronmoring").toString().equals("")||  t.get("openid").toString().equals("")){
////                   return new CronTrigger("0 0 0 31 12 ? ").nextExecutionTime(triggerContext);
////               }else {
////               pushService.goodMorningtest(t.get("openid").toString(),t.get("cronmoring").toString());
//                return new CronTrigger(t.get("cronmoring").toString()).nextExecutionTime(triggerContext);
////               }
//                //return new CronTrigger(t.get("cronmoring").toString()).nextExecutionTime(triggerContext);
//
//            });
////            TriggerTask triggerTask2 = new TriggerTask(()->{
////                System.out.println("执行定时任务："+ LocalDateTime.now().toLocalTime());
////                // pushService.goodMorning();
////
////            },triggerContext -> {
////               // System.out.println("执行Cron晚上的:"+t.get("cornnight").toString()+",id="+t.get("id").toString()+",openid="+t.get("openid").toString());
////                //如果需要动态的指定当前定时任务的执行corn。这里可以增加一步，查询数据库操作。如果任务corn不需要精确修改，corn可进行缓存。到期在去查询数据库。这里根据读者的需求自行取舍。
//////                if(t.get("cornnight").toString() .equals("") ||  t.get("openid").toString().equals("")){
//////                    return new CronTrigger("0 0 0 31 12 ? ").nextExecutionTime(triggerContext);
//////                }else {
//////                    pushService.goodNighttest(t.get("openid").toString(),t.get("cornnight").toString());
//////                    return new CronTrigger(t.get("cornnight").toString()).nextExecutionTime(triggerContext);
//////                }
////                return new CronTrigger("0 0 0 31 12 ? ").nextExecutionTime(triggerContext);
////
////            });
//            list.add(triggerTask);
//            //  list.add(triggerTask2);
//        });
//        //将任务列表注册到定时器
////        if(maps.equals(null)|| maps==null||maps.equals("")||maps.size()==0){
////            System.out.println("不进来");
////            return;
////        }else {
//        System.out.println("进阿里");
//        scheduledTaskRegistrar.setTriggerTasksList(list);
//        this.taskRegistrar = scheduledTaskRegistrar;
//
////
////        maps.forEach(t-> {
////            scheduledTaskRegistrar.addTriggerTask(new Runnable() {
////                @Override
////                public void run() {
////                    System.out.println(("test") + LocalDateTime.now().toLocalTime());
////                    //业务逻辑
////                }
////            }, new Trigger() {
////                @Override
////                public Date nextExecutionTime(TriggerContext triggerContext) {
////                    cron.setCronmoring(cornTaskMapper.selectAllTask1());
////                    System.out.println(cron.getCronmoring() + "11111111111");
////                    List<String> list = cornTaskMapper.selectList();
//////                    for (int i=0;list.)
////                    if ("".equals(cron.getCronmoring()) || cron.getCronmoring() == null) {
////                        return null;
////                    }
////                    //如果corn发生变化，返回新的corn
////                    CronTrigger trigger = new CronTrigger(cron.getCronmoring());
////                    System.out.println(cron.getCronmoring() + "1111111111122");
////                    return trigger.nextExecutionTime(triggerContext);
////                }
////            });
//
//
//    }
//}
//
//
////    /**
////     * 获取任务列表
////     */
////    private Set<ScheduledFuture<?>> getScheduledFutures() {
////        if (scheduledFutures == null) {
////            try {
////                    scheduledFutures = (Set<ScheduledFuture<?>>) Pro.getProperty(taskRegistrar, "scheduledTasks");
////            } catch (NoSuchFieldException e) {
////                throw new SchedulingException("not found scheduledFutures field.");
////            }
////        }
////        return scheduledFutures;
////    }
//
//
//   // }
